<?php
namespace app\api\service;
use app\api\service\Token;
use think\Request;
use app\api\model\Alliance;
use app\api\model\Account;
use think\Cache;
class AccountService {

    // 登录
    public function login() {
       
        Request::instance()->isPost() || die(show(0, '错误', [], 200));

        //获取参数
        $param = Request::instance()->param();
        
        $user  = Account::with(['falliance'])
                ->where(['username' => $param['userName']])
                ->where(['password' => md5($param['password'])])
                ->find();

        //检测用户是否存在
    	if( is_null($user) ){
            return show(0, '用户名或者密码错误', [], 200);
        }
        // 正常客户
        if($user['status'] != 4) {
            return show(0, '当前门店后台未审核', [], 200);
        }

        // 保存token
        $token =  Token::grantToken(json_decode($user), 'XXX');
        return show(1, '登录成功', [
            'user' => $user,
            'token' => $token], 200);

    }

    public function logout() {
        $token = Request::instance()->header('token');
        cache($token, NULL);
        return show(1, 'ok', [], 200);
    }

    // 获取员工信息
    public function page($page = 1, $size = 10){
        $data 	= Request::instance()->get();
        $where 	= [];
        if (!isset($data['alliance_id']) || !is_numeric($data['alliance_id'])) {
            return [];
        }

        $where['alliance_id'] = $data['alliance_id'];  // 当前 加盟商员工
        $where['type'] = 1; 
        empty($data['username']) || $where['username'] = ['like','%'.$data['username']];

        $obj = Account::where($where)
        ->paginate($size, false, ['page' => $page]);

        if ($obj->isEmpty()){
            return [];
        }
        return $obj->toArray();
    }

    public function save(){ 
        $param = Request::instance()->param();	//获取参数

        if (!isset($param['id']) || !is_numeric($param['id']))
        {
            // 新增
            $obj = new Account;

            try {
                $obj->username = $param['username'];
                $obj->password = md5($param['password']);
                $obj->alliance_id = $param['alliance_id'];
                
            }catch(Exption $e) {
                return show(0, '修改失败', [], 200);
            }
            if( $obj->save()){
                return show(1, '新增成功', [], 200);
            }
        }else {
            // 更新
            $obj = Account::get($param['id']);
            try {
                $obj->username = $param['username'];
            }catch(Exption $e) {
                return show(0, '修改失败', [], 200);
            }
            // 检测错误
            if( $obj->save()){
                return show(1, '修改成功', [], 200);
            }
        }
        
        return show(0, '修改失败', [], 200);
    }

    public function resetUser($id) {
        // 更新
        $obj = Account::get($id);
        try {
            $obj->password = md5('hwl123');
        }catch(Exption $e) {
            return show(0, '重置失败', [], 200);
        }
        // 检测错误
        if( $obj->save()){
            return show(1, '重置成功', [], 200);
        }

        return show(0, '重置失败', [], 200);
    }

    public function delete($id){

    	if( Account::destroy($id, true) ){
            return show(1, '删除成功', [], 200);
        }
        
        return show(0, '删除失败', [], 200);
    }
}